<?php
/*
 * code to display datagrid of customer records called asynchronously
*/
//check session
include("../inc_files/utils/checksession.php");
// Create connection
include("../inc_files/utils/dbconnection.php");
//query for all customers
$query ="SELECT * FROM customers WHERE Deleted != 1";
$result = $mysqli->query( $query );

//count the number of returned results
$resultCount = $result->num_rows;

//if there are some results returned create a table showing the results
if( $resultCount > 0){
	$rownumber = 0;

	echo "<table class='datagrid'>
			<tr class=\"datagrid headerrow\">
			<th style=\"width:45%\"><a href='#'>Company</a></th>
			<th style=\"width:40%\"><a href='#'>City</a></th>
			<th style=\"width:5%\">Info</th>
			<th style=\"width:5%\">Edit</th>
			<th style=\"width:5%\">Delete</th>
			</tr>";

	while($row = mysqli_fetch_array($result))
	{
		if($rownumber %2 == 0)
		{
			echo "<tr class=\"datagrid evenrow\">";
		}
		else
		{
			echo "<tr class=\"datagrid oddrow\">";
		}


		echo "<td style=\"width:45%\">" . $row['Company'] . "</td>";
		echo "<td style=\"width:40%\">" . $row['City'] . "</td>";
		echo "<td style=\"width:5%\"><input type=\"image\"  height=\"32\" width=\"32\"  src=\"../images/view_32.png\" alt=\"View records for ". $row['Company'] ."\" onclick=\"window.location.href='customerdetail.php?customerid="  . $row['CustomerID'] . "'\"></td>";
		echo "<td style=\"width:5%\"><input type=\"image\"  height=\"32\" width=\"32\"  src=\"../images/pencil_32.png\" alt=\"Edit records for ". $row['Company'] ."\" onclick=\"window.location.href='editcustomer.php?customerid="  . $row['CustomerID'] . "'\"></td>";
		echo "<td style=\"width:5%\"><img height=\"32\" width=\"32\"  src=\"../images/edit-delete-2-32.png\" alt=\"Delete records for ". $row['Company'] ."\" onclick=\"deleterecord('"  . $row['CustomerID'] . "')\" style=\"cursor:pointer\"></td>";
		echo "</tr>";
		$rownumber++;
	}
	echo "<tr class=\"datagrid footer\">
			<td colspan=\"2\" style=\"width:70%\"><a href=\"/f21na/customers/newcustomer.php\">Add new customer
			<img src=\"../images/add_32.png\" style=\"border:none\" height=\"32\" width=\"32\" alt=\" Add new customer record.\" ></a>
			</td>
			<td colspan=\"3\" style=\"width:30%\"></td>
			</tr>";
	echo "</table>";
}
//if no results returned say so.
else
{

	echo"No records returned";
}
//tidy up database connection
$mysqli->close();


?>